<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>AK JOB</title>
    <link rel="icon" type="image/x-icon" href="../../../layuiadmin/style/res/favicon.png"/>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card layui-padding-3" id="applicationForm">
        <div class="layui-form" lay-filter="applicationForm">
            <div class="layui-form-item layui-hide" id="idDev">
                <label class="layui-form-label">命名空间ID</label>
                <div class="layui-input-block">
                    <div class="layui-input-group">
                        <input type="text" name="id" lay-verify="max32" placeholder="命名空间ID"
                               autocomplete="off"
                               lay-verType="tips"
                               class="layui-input"/>
                        <div class="layui-input-split layui-input-suffix">
                            不填则自动生成
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">命名空间名</label>
                <div class="layui-input-block">
                    <input type="text" name="name" lay-verify="required|max100" placeholder="请输入命名空间名"
                           autocomplete="off"
                           lay-verType="tips"
                           class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">描述</label>
                <div class="layui-input-block">
                    <input type="text" name="namespaceDes" lay-verify="max100" placeholder="请输入描述"
                           autocomplete="off"
                           lay-verType="tips"
                           class="layui-input"/>
                </div>
            </div>
            <input type="button" class="layui-btn layui-hide" style="width: 100%" lay-submit
                   lay-filter="namespaceFormSubmit"
                   id="namespaceFormSubmit" value="保存">
        </div>
    </div>
</div>

<script src="../../../layuiadmin/layui/layui.js"></script>
<script src="../../../layuiadmin/lib/juicer-min.js"></script>
<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'roles', 'table', 'setter', 'auth', 'AppHelper', 'namespace'], function () {
        var $ = layui.$
            , form = layui.form
            , roles = layui.roles
            , setter = layui.setter
            , table = layui.table
            , namespace = layui.namespace
            , AppHelper = layui.AppHelper
            , LayerPage = AppHelper.getLayerPage()
        ;
        //解析当前路由信息
        var router = layui.router();
        var action = router.search.action;
        var id = router.search.id;

        form.verify({
            max100: function (value, elem) {
                if (value && value.length > 100) {
                    return '不能大于 100 个字符';
                }
            },max32: function (value, elem) {
                if (value && value.length > 100) {
                    return '不能大于 32 个字符';
                }
            }
        });

        var refreshParentTable = function () {
            parent.layui.table.reload("LAY-role-manage");
        }
        switch (action) {
            case "add":
                $("#idDev").removeClass("layui-hide")
                form.on("submit(namespaceFormSubmit)", function (obj) {
                    var field = obj.field;
                    field.id = id
                    //保存
                    var layerPageLoad = LayerPage.load();
                    namespace.methods.add(field, 1)
                        .done(function (res) {
                            if (res.code == "200") {
                                layerPageLoad.close();
                                parent.layer.msg("新增成功！", {icon: 1}, function () {
                                })
                                refreshParentTable();
                                LayerPage.close();
                            }
                        })
                        .fail(function () {
                            layerPageLoad.close();
                        });
                })
                break;
            case "edit":
                $.when(namespace.methods.info(id))
                    .done(function (adminReq) {
                        var adminInfo = adminReq.data;
                        //回显角色名称
                        form.val("applicationForm", adminInfo)
                        form.on("submit(namespaceFormSubmit)", function (obj) {
                            var field = obj.field;
                            field.id = id
                            //保存
                            var layerPageLoad = LayerPage.load();
                            namespace.methods.update(field)
                                .done(function (res) {
                                    if (res.code == "200") {
                                        layerPageLoad.close();
                                        parent.layer.msg("修改成功！", {icon: 1}, function () {
                                        })
                                        refreshParentTable();
                                        LayerPage.close();
                                    }
                                })
                                .fail(function () {
                                    layerPageLoad.close();
                                });
                        })
                    })
                break;
        }


    });
</script>
</body>
</html>
